package org.linlinjava.litemall.db.service.distribution;

import com.github.pagehelper.PageHelper;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.linlinjava.litemall.db.dao.DisMemberAmountMapper;
import org.linlinjava.litemall.db.dao.distribution.DisMemberAmountDao;
import org.linlinjava.litemall.db.domain.DisMemberAmount;
import org.linlinjava.litemall.db.domain.DisMemberAmountExample;
import org.linlinjava.litemall.db.domain.DisMemberInfo;
import org.linlinjava.litemall.db.domain.DisMemberInfoExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 账户管理Dao
 *
 * @author xiaojiang
 * @Date 2018-05-08 21:16:47
 */
@Service
@Transactional
public class DisMemberAmountService {


    @Resource
    DisMemberAmountMapper disMemberAmountMapper;

    @Resource
    DisMemberAmountDao disMemberAmountDao;



    @Autowired
    DisMemberInfoService disMemberInfoService;


    public List<DisMemberAmount> selectList(String username, String userId, Integer page, Integer limit) {
        DisMemberAmountExample example = new DisMemberAmountExample();
        DisMemberAmountExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(username)) {
            criteria.andDisUserNameLike("%" + username + "%");
        }
        if (!StringUtils.isEmpty(userId)) {
            criteria.andDisUserIdEqualTo(userId);
        }

        PageHelper.startPage(page, limit);
        return disMemberAmountMapper.selectByExample(example);
    }

    public int countSeletive(String username, String userId, Integer page, Integer limit) {
        DisMemberAmountExample example = new DisMemberAmountExample();
        DisMemberAmountExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(username)) {
            criteria.andDisUserNameLike("%" + username + "%");
        }
        if (!StringUtils.isEmpty(userId)) {
            criteria.andDisUserIdEqualTo(userId);
        }
        PageHelper.startPage(page, limit);
        return (int)disMemberAmountMapper.countByExample(example);
    }

    public DisMemberAmount getByuserId(Integer userId) {
        DisMemberAmountExample example = new DisMemberAmountExample();
        DisMemberAmountExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(userId)) {
            criteria.andDisUserIdEqualTo(userId.toString());
        }else{
            return null;
        }

        List<DisMemberAmount> amount = disMemberAmountMapper.selectByExample(example);
        if(amount!=null && amount.size()>0){
            return amount.get(0);
        }
        return null;
    }

    public void update(DisMemberAmount amount) {
        disMemberAmountMapper.updateByPrimaryKey(amount);
    }
    public void insert(DisMemberAmount amount) {
        disMemberAmountMapper.insertSelective(amount);
    }



}
